package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.FoodMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Food;
import com.itheima.service.CheckItemService;
import com.itheima.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = FoodService.class)
@Transactional
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodMapper foodMapper;
    //向膳食表中导入数据
    @Override
    public void add(ArrayList<Food> foodList) {
        for (Food food : foodList) {
            //根据膳食信息名称查询是否有这条膳食
            String foodName = food.getFoodName();
            //膳食名称非空
            if (foodName!=null&&foodName.length()>0) {
                Integer count = foodMapper.findCountByFoodName(foodName);
                if (count > 0) {
                    //查找到进行修改操作
                    foodMapper.update(food);
                } else {
                    //没有查找到进行添加
                    foodMapper.add(food);
                }
            }
        }
    }
    //分页查询功能
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        //获取页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取每页显示条数
        Integer pageSize = queryPageBean.getPageSize();
        //获取查询条件
        String queryString = queryPageBean.getQueryString();

        //分页插件进行分页
        PageHelper.startPage(currentPage,pageSize);
        //调用持久层的方法
        Page<Food> page = foodMapper.findByPage(queryString);
        //返回集合
        List<Food> list = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, list);
    }
    //获取当前页数据
    @Override
    public List<Food> findCurrentPage(QueryPageBean queryPageBean) {
        //获取页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取每页显示条数
        Integer pageSize = queryPageBean.getPageSize();
        //获取查询条件
        String queryString = queryPageBean.getQueryString();
        //分页插件进行分页
        PageHelper.startPage(currentPage,pageSize);
        //调用持久层的方法
        List<Food> list = foodMapper.findByPage(queryString);
        //返回集合
        return list;

    }
    //根据id删除功能
    @Override
    public void deleteById(Integer id) {
         foodMapper.deleteById(id);;
    }

    @Override
    public List<Food> findAll() {
        return foodMapper.findAll();
    }
}
